package cn.enzo.yun.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.enzo.utils.controller.CommonController;
import cn.enzo.yun.domain.Store;
import cn.enzo.yun.service.IStoreService;

@Controller
@RequestMapping(value = StoreController.url_path)
public class StoreController extends CommonController<Store> {

	public final static String url_path = "/store";
	private static final Logger logger = Logger.getLogger(StoreController.class);

	@Autowired
	private IStoreService storeService;

	@RequestMapping(value = "/list")
	public ModelAndView list(ModelAndView mav, Store entity, @RequestParam(defaultValue = "1") Integer pageNum)
			throws Exception {

		logger.info("store list ...");

		PageHelper.startPage(pageNum, 10);
		List<Store> storelist = storeService.queryByCondition(entity);
		PageInfo<Store> pageInfo = new PageInfo<>(storelist);
		mav.addObject(PAGE, pageInfo);
		mav.setViewName("store/storelist");
		return mav;
	}

	@RequestMapping(value = "/delete")
	public ModelAndView delete(ModelAndView mav, String id, Integer pageNum) {

		logger.info("store delete ...");
		try {
			storeService.deleteBykey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		mav.setViewName(REDIRECT + url_path + "/list?pageNum=" + pageNum);
		return mav;
	}
}
